package com.avalonsql.core.db.managers.impl;

import com.avalonsql.core.db.DBException;
import com.avalonsql.core.db.DBManager;
import com.avalonsql.core.db.connection.ConnectionWrapper;
import com.avalonsql.core.db.managers.api.IStoredProcedureColumnsManager;
import com.avalonsql.core.db.objects.api.IRootObject;
import com.avalonsql.core.db.objects.impl.StoredProcedure;
import com.avalonsql.core.db.objects.impl.StoredProcedureColumn;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.TreeSet;

public class StoredProcedureColumnsManager extends ConnectionWrapperManager implements IStoredProcedureColumnsManager {
    
    public StoredProcedureColumnsManager(ConnectionWrapper connectionWrapper) {
        super(connectionWrapper);
    }

    public Collection<StoredProcedureColumn> loadAll(StoredProcedure parent) throws DBException {
        if (parent == null) {
            throw new IllegalArgumentException("parent = null");
        }

        Collection<StoredProcedureColumn> values = new TreeSet<StoredProcedureColumn>();
        ResultSet rs = null;
        try {
            IRootObject rootObject = parent.getParent();
            rs = getDatabaseMetaData().getProcedureColumns(getCatalog(rootObject), getSchema(rootObject), parent.getName(), "%");
            while (rs.next()) {
                values.add(
                    	// todo-a
                        new StoredProcedureColumn(getStringValue(rs, "PROCEDURE_SCHEM"), getStringValue(rs, "PROCEDURE_CAT"),
                                getStringValue(rs, "COLUMN_NAME"), getStringValue(rs, "TYPE_NAME"), getBooleanValue(rs, "NULLABLE"), getIntValue(rs, "LENGTH"), parent
                        )
                );
            }
        } catch (SQLException e) {
            throw new DBException(e);
        } finally {
            DBManager.closeResultSet(rs);
        }
        return values;
    }

    public void create(StoredProcedureColumn dbObject) throws DBException {
        //todo
    }

    public void update(StoredProcedureColumn dbObject) throws DBException {
        //todo
    }

    public void delete(StoredProcedureColumn dbObject) throws DBException {
        //todo
    }
}
